import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/view/course/course_view.dart';
import 'package:kxy_flutter_app/view/examination/exam_view.dart';
import 'package:kxy_flutter_app/view/home/home_view.dart';
import 'package:kxy_flutter_app/view/root/view_model/app_info_model.dart';
import '../news/news_view.dart';
import 'package:provider/provider.dart';
import '../my/my_view.dart';
import 'view_model/navigation_model.dart';

class TabView extends StatelessWidget {
  final pageList = [
    HomeView(),
    NewsView(),
    Builder(builder: (cxt) => ExaminationView()),
    CourseView(),
    MyView(),
  ];
  @override
  Widget build(BuildContext context) {
    var appModel = Provider.of<AppInfoModel>(context, listen: false);
    appModel.loadAppInfo();
    return WillPopScope(
      child: Consumer<NavigationModel>(
        builder: (cxt, navModel, child) {
          return GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: Scaffold(
              body: PageView(
                controller: navModel.pageController,
                children: pageList,
                physics: NeverScrollableScrollPhysics(),
                onPageChanged: (index) => navModel.changeSelectIndex(index),
              ),
              bottomNavigationBar: Stack(
                children: [
                  BottomNavigationBar(
                    backgroundColor: Colors.white,
                    type: BottomNavigationBarType.fixed,
                    items: buildNavItems(navModel.items),
                    onTap: (index) => navModel.changeSelectIndex(index),
                    currentIndex: navModel.index,
                    unselectedItemColor: Color.fromRGBO(153, 153, 153, 1),
                    selectedItemColor: Color.fromRGBO(40, 126, 237, 1),
                    unselectedFontSize: 10,
                    selectedFontSize: 11,
                  ),
                  if (navModel.certCount > 0)
                    Positioned(
                      right: 15,
                      top: 10,
                      child: dot(),
                    )
                ],
              ),
            ),
          );
        },
      ),
      onWillPop: () async {
        var confirm = await confirmExits(context);
        return confirm;
      },
    );
  }

  Widget dot() {
    return Container(
      height: 8,
      width: 8,
      decoration: BoxDecoration(
        color: Colors.red,
        shape: BoxShape.circle,
      ),
    );
  }

  Future<bool> confirmExits(BuildContext context) {
    return showDialog(
      context: context,
      builder: (cxt) {
        return AlertDialog(title: Text('提示'), content: Text('您确认要离开吗？'), actions: [
          FlatButton(
            child: Text('取消'),
            onPressed: () {
              Navigator.pop(context, false);
            },
          ),
          FlatButton(
            child: Text('确认'),
            onPressed: () {
              Navigator.pop(context, true);
            },
          ),
        ]);
      },
    );
  }

  List<BottomNavigationBarItem> buildNavItems(List<NavigationItem> items) {
    var result = new List<BottomNavigationBarItem>();

    for (var item in items) {
      result.add(
        BottomNavigationBarItem(
          title: Padding(
            padding: EdgeInsets.only(top: 2),
            child: Text(
              item.title,
              style: TextStyle(fontSize: 11),
            ),
          ),
          icon: Image(
            image: AssetImage(item.icon),
            width: 24,
            height: 24,
          ),
          activeIcon: Image(
            image: AssetImage(item.activeIcon),
            width: 24,
            height: 24,
          ),
        ),
      );
    }
    return result;
  }
}
